﻿using Microsoft.Win32;
using System;
using System.Diagnostics;
using System.IO;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

namespace _7._1
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            PopulateTreeView(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile));
            listView.MouseDoubleClick += ListView_MouseDoubleClick; // 添加双击事件处理
        }

        private void PopulateTreeView(string rootPath)
        {
            DirectoryInfo rootDirectory = new DirectoryInfo(rootPath);
            TreeViewItem rootItem = new TreeViewItem
            {
                Header = rootDirectory.Name,
                Tag = rootDirectory.FullName
            };
            rootItem.Items.Add("*"); // Placeholder to indicate that it has subitems
            rootItem.Expanded += RootItem_Expanded;
            treeView.Items.Add(rootItem);
        }

        private void RootItem_Expanded(object sender, RoutedEventArgs e)
        {
            TreeViewItem rootItem = (TreeViewItem)sender;
            if (rootItem.Items.Count == 1 && rootItem.Items[0] is string && (string)rootItem.Items[0] == "*")
            {
                rootItem.Items.Clear();
                string folderPath = (string)rootItem.Tag;
                try
                {
                    foreach (var subItem in Directory.GetDirectories(folderPath))
                    {
                        TreeViewItem subFolderItem = new TreeViewItem
                        {
                            Header = new DirectoryInfo(subItem).Name,
                            Tag = subItem
                        };
                        subFolderItem.Items.Add("*");
                        subFolderItem.Expanded += SubFolderItem_Expanded;
                        rootItem.Items.Add(subFolderItem);
                    }
                }
                catch (UnauthorizedAccessException)
                {
                    // Handle unauthorized access (e.g., protected system folders)
                }
            }
        }

        private void SubFolderItem_Expanded(object sender, RoutedEventArgs e)
        {
            TreeViewItem subFolderItem = (TreeViewItem)sender;
            if (subFolderItem.Items.Count == 1 && subFolderItem.Items[0] is string && (string)subFolderItem.Items[0] == "*")
            {
                subFolderItem.Items.Clear();
                string folderPath = (string)subFolderItem.Tag;
                try
                {
                    foreach (var subItem in Directory.GetDirectories(folderPath))
                    {
                        TreeViewItem subSubFolderItem = new TreeViewItem
                        {
                            Header = new DirectoryInfo(subItem).Name,
                            Tag = subItem
                        };
                        subSubFolderItem.Items.Add("*");
                        subSubFolderItem.Expanded += SubFolderItem_Expanded;
                        subFolderItem.Items.Add(subSubFolderItem);
                    }
                    foreach (var file in Directory.GetFiles(folderPath))
                    {
                        TreeViewItem fileItem = new TreeViewItem
                        {
                            Header = Path.GetFileName(file),
                            Tag = file
                        };
                        subFolderItem.Items.Add(fileItem);
                    }
                }
                catch (UnauthorizedAccessException)
                {
                    // Handle unauthorized access (e.g., protected system folders)
                }
            }
        }

        private void TreeView_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
        {
            if (e.NewValue is TreeViewItem selectedItem && selectedItem.Tag is string folderPath)
            {
                listView.Items.Clear(); // 清空列表
                DirectoryInfo dirInfo = new DirectoryInfo(folderPath);
                foreach (var item in dirInfo.GetDirectories())
                {
                    listView.Items.Add(new { Name = item.Name, Type = "Folder", Path = item.FullName });
                }
                foreach (var item in dirInfo.GetFiles())
                {
                    listView.Items.Add(new { Name = item.Name, Type = "File", Path = item.FullName });
                }
            }
        }

        private void ListView_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            if (listView.SelectedItem != null)
            {
                dynamic selectedItem = listView.SelectedItem;
                string filePath = selectedItem.Path;
                if (selectedItem.Type == "File" && Path.GetExtension(filePath).Equals(".txt", StringComparison.OrdinalIgnoreCase))
                {
                    MessageBox.Show($"Opening {filePath}");
                    // 打开txt文件的逻辑
                    try
                    {
                        Process.Start(filePath);
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show($"Error opening file: {ex.Message}");
                    }
                }
            }
        }

        private void OpenFile_Click(object sender, RoutedEventArgs e)
        {
            if (listView.SelectedItem != null)
            {
                dynamic selectedItem = listView.SelectedItem;
                string filePath = selectedItem.Path;
                if (selectedItem.Type == "File" && Path.GetExtension(filePath).Equals(".txt", StringComparison.OrdinalIgnoreCase))
                {
                    MessageBox.Show($"Opening {filePath}");
                    // 打开txt文件的逻辑
                    try
                    {
                        Process.Start(filePath);
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show($"Error opening file: {ex.Message}");
                    }
                }
            }
        }
    }
}